using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using SadlandVillage.Core;
using SadlandVillage.Core.Helper;
using SadlandVillage.Minigames.Restaurant.Helper;

namespace SadlandVillage.Minigames.Restaurant
{
    public enum GameModes
    {
        Beginning,
        NewLevel,
        Running,
        Stopped,
        PlateGameOver,
        GameOver
    }
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Restaurant : MiniGame /*Microsoft.Xna.Framework.Game*/
    {
        private GraphicsDevice graphics;
        private SpriteFont font;
        private Plates actualPlate;
        private GameModes gameMode;
        private Dish dish;

        private int introTimer = 0;

        private bool firstIntro = true;

        public override event BackToMapEventHandler GameCancelled;

        public Restaurant(ContentManager content,
            SpriteBatch spriteBatch)
            : base(content, spriteBatch)
        {
            graphics = spriteBatch.GraphicsDevice;
            //content.RootDirectory = "Content";
            GameActivated += new EventHandler(Initialize);
            GameDeactivated += new EventHandler(Finalize);
        }

        private void Initialize(object sender, EventArgs e)
        {
            gameMode = GameModes.Beginning;
            actualPlate = Plates.Pizza;
        }

        private void Finalize(object sender, EventArgs e)
        {
            GlobalContainer.components.Clear();
            StaticContainer.platesXtotal.Clear();
            this.GameCancelled(0);
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        public override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.

            StaticContainer.pizzaEmpty = content.Load<Texture2D>(@"Images\pizza_contour");
            StaticContainer.pizzaFull = content.Load<Texture2D>(@"Images\pizza_full");

            StaticContainer.chickenEmpty = content.Load<Texture2D>(@"Images\chicken_contour");
            StaticContainer.chickenFull = content.Load<Texture2D>(@"Images\chicken_full");

            StaticContainer.feijoadaEmpty = content.Load<Texture2D>(@"Images\feijoada_contour");
            StaticContainer.feijoadaFull = content.Load<Texture2D>(@"Images\feijoada_full");

            StaticContainer.fishEmpty = content.Load<Texture2D>(@"Images\fish_contour");
            StaticContainer.fishFull = content.Load<Texture2D>(@"Images\fish_full");

            StaticContainer.foieGrasEmpty = content.Load<Texture2D>(@"Images\foieGras_contour");
            StaticContainer.foieGrasFull = content.Load<Texture2D>(@"Images\foieGras_full");

            StaticContainer.lobsterEmpty = content.Load<Texture2D>(@"Images\lobster_contour");
            StaticContainer.lobsterFull = content.Load<Texture2D>(@"Images\lobster_full");

            StaticContainer.sandwichEmpty = content.Load<Texture2D>(@"Images\sandwich_contour");
            StaticContainer.sandwichFull = content.Load<Texture2D>(@"Images\sandwich_full");

            StaticContainer.sushiEmpty = content.Load<Texture2D>(@"Images\sushi_contour");
            StaticContainer.sushiFull = content.Load<Texture2D>(@"Images\sushi_full");

            StaticContainer.blackPoint = content.Load<Texture2D>(@"Images\blackPoint");
            StaticContainer.cursor = content.Load<Texture2D>(@"Images\woodenspoom");

            StaticContainer.intro = content.Load<Texture2D>(@"Images\intro");
            StaticContainer.intro2 = content.Load<Texture2D>(@"Images\intro2");
            StaticContainer.kitchen = content.Load<Texture2D>(@"Images\kitchen");
            StaticContainer.kitchenEmpty = content.Load<Texture2D>(@"Images\kitchenEmpty");
            StaticContainer.gameOverLose = content.Load<Texture2D>(@"Images\gameOverLose");
            StaticContainer.gameOverWin = content.Load<Texture2D>(@"Images\gameOverWin");

            StaticContainer.getReadyFont = content.Load<SpriteFont>("GetReady");
            font = content.Load<SpriteFont>("gameFont");

            if (GlobalContainer.services.GetService(typeof(SpriteFont)) == null)
            {
                GlobalContainer.services.AddService(typeof(SpriteFont), font);
            }
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        public override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                FinishGame();

            switch (gameMode)
            {
                case GameModes.Beginning:
                    introTimer++;
                    if (introTimer == 200)
                    {
                        if (!firstIntro)
                        {
                            gameMode = GameModes.NewLevel;
                            firstIntro = true;
                        }
                        else
                        {
                            firstIntro = false;
                        }
                        introTimer = 0;
                    }
                    break;
                case GameModes.NewLevel:
                    gameMode = GameModes.Stopped;
                    StartNewPlate();
                    if (gameMode == GameModes.Stopped)
                    {
                        gameMode = GameModes.Running;
                    }
                    break;
                case GameModes.Running:
                    break;
                case GameModes.Stopped:
                    break;
                case GameModes.GameOver:
                    if (Input.KeyPressed(Keys.Escape))
                    {
                        FinishGame();
                    }

                    if (Input.KeyPressed(Keys.Enter))
                    {
                        gameMode = GameModes.NewLevel;
                        actualPlate = Plates.Pizza;
                        StaticContainer.platesXtotal.Clear();
                    }
                    break;
                default:
                    break;
            }

            //base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Draw(GameTime gameTime)
        {
            graphics.Clear(Color.CornflowerBlue);

            if (gameMode == GameModes.Beginning)
            {
                spriteBatch.Begin();
                /*spriteBatch.DrawString(font, "Mr Macarroni, a very hungry man, is at Ms Clotilde's restaurant. She's\n" +
                                             "a lonely poor woman, needing a good and quick cook, to prepare the dishes\n" + 
                                             "that Mr. Macarroni wants. Please, help this old woman to earn the money!", new Vector2(30, 30), Color.Black);*/
                Texture2D image;
                if (firstIntro)
                {
                    image = StaticContainer.intro;
                }
                else
                {
                    image = StaticContainer.intro2;
                }
                spriteBatch.Draw(image, Vector2.Zero, Color.White);

                spriteBatch.End();
            }
        }

        private void FinishGame()
        {
            this.Active = false;
        }

        private void StartNewPlate()
        {
            bool finish = false;

            if (dish != null)
            {
                dish.Dispose();
            }

            switch (actualPlate)
            {
                case Plates.Pizza:
                    dish = new Dish(StaticContainer.pizzaEmpty, StaticContainer.pizzaFull, 6000, actualPlate, Plates.Chicken, spriteBatch);
                    break;
                case Plates.Chicken:
                    dish = new Dish(StaticContainer.chickenEmpty, StaticContainer.chickenFull, 14000, actualPlate, Plates.Feijoada, spriteBatch);
                    break;
                case Plates.Feijoada:
                    dish = new Dish(StaticContainer.feijoadaEmpty, StaticContainer.feijoadaFull, 13000, actualPlate, Plates.Fish, spriteBatch);
                    break;
                case Plates.Fish:
                    dish = new Dish(StaticContainer.fishEmpty, StaticContainer.fishFull, 19000, actualPlate, Plates.Foie_Gras, spriteBatch);
                    break;
                case Plates.Foie_Gras:
                    dish = new Dish(StaticContainer.foieGrasEmpty, StaticContainer.foieGrasFull, 16000, actualPlate, Plates.Lobster, spriteBatch);
                    break;
                case Plates.Lobster:
                    dish = new Dish(StaticContainer.lobsterEmpty, StaticContainer.lobsterFull, 18000, actualPlate, Plates.Sandwich, spriteBatch);
                    break;
                case Plates.Sandwich:
                    dish = new Dish(StaticContainer.sandwichEmpty, StaticContainer.sandwichFull, 10000, actualPlate, Plates.Sushi, spriteBatch);
                    break;
                case Plates.Sushi:
                    dish = new Dish(StaticContainer.sushiEmpty, StaticContainer.sushiFull, 20000, actualPlate, Plates.None, spriteBatch);
                    break;
                case Plates.None:
                    finish = true;
                    break;
                default:
                    break;
            }

            if (!finish)
            {
                dish.PlateLoser += new Dish.PlateLoserEventHandle(Plate_PlateLoser);
                GlobalContainer.components.Add(dish);
            }
            else
            {
                gameMode = GameModes.GameOver;
                GlobalContainer.components.Add(new GameOver(GlobalContainer.game));
                gameMode = GameModes.GameOver;
            }
        }

        #region Events
        void Plate_PlateLoser(Plates nextPlate)
        {
            gameMode = GameModes.NewLevel;
            actualPlate = nextPlate;
        }

        #endregion

    }
}
